<template>
	<el-form ref="ruleFormRef" :model="ruleForm"  label-width="120px" class="white-module global-ruleForm" :size="formSize">
		<el-form-item label="账户昵称" ><el-input v-model="ruleForm.nickname" placeholder="请输入" /></el-form-item>
		<el-form-item label="集团名称" ><el-input disabled="disabled" v-model="ruleForm.company" placeholder="请输入" /></el-form-item>
		<el-form-item label="账户名称" ><el-input disabled="disabled" v-model="ruleForm.account" placeholder="请输入" />
      <a class="no_pwd" v-if="userInfo.type===1" tag="a" @click="showResetPassDialog()">忘记密码</a>
    </el-form-item>
		<el-form-item label="税务号"><el-input v-model="ruleForm.taxNo" placeholder="请输入" /></el-form-item>
		<el-form-item label="集团地址"><el-input disabled="disabled" v-model="ruleForm.address" placeholder="请输入" /></el-form-item>
		<el-form-item label="负责人姓名"><el-input v-model="ruleForm.contacts" placeholder="请输入" /></el-form-item>
		<el-form-item label="联系电话" ><el-input v-model="ruleForm.phone" placeholder="请输入" /></el-form-item>
		<el-form-item label="联系邮箱"><el-input v-model="ruleForm.mail" placeholder="请输入" /></el-form-item>

		<el-form-item class="btns" v-if="userInfo.type===1">
			<el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
			<el-button @click="resetForm(ruleFormRef)">取消</el-button>
		</el-form-item>
	</el-form>

  <!--重置密码-->
  <el-dialog title="重置密码" v-model="ruleForm.showResetPass">
    <el-form :model="resetPassForm">
      <el-form-item label="输入新密码" :label-width="formLabelWidth">
        <el-input v-model="resetPassForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="再次输入密码" :label-width="formLabelWidth">
        <el-input v-model="resetPassForm.password2" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="ruleForm.showResetPass = false">取 消</el-button>
      <el-button type="primary" @click="resetPass">确 定</el-button>
    </div>
  </el-dialog>


</template>
<script lang="ts" setup>
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import type { FormInstance, UploadProps } from "element-plus";
import baseService from "@/service/baseService";
import {getCache} from "@/utils/cache";
const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  id: null,
  address: "",
  nickname: "",
  company: "",
  account: "",
  accountId: null,
  contacts: "",
  phone: "",
  mail: "",
  taxNo: "",
  showResetPass: false
});

const resetPassForm = reactive({
  id: null,
  password: '',
  password2: ''
});

const userInfo = getCache('CacheToken', { isSessionStorage: true }, {})



const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      //调用保存企业信息
      baseService
          .post("/fii/front/center/company/update", ruleForm)
          .then((res) => {
                if (res.code === 0) {
                  ElMessage.success("保存成功");
                }
              }
          );

    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};


const company = reactive({
      info: {}
    }
);

const queryCompanyInfo = () => {

    baseService
        .post("/fii/front/center/company/info", {})
        .then((res) => {
              if (res.code === 0) {
                let info = res.data;
                company.info = info;
                if(info){
                  ruleForm.id = info.id;
                  ruleForm.address = info.address;
                  ruleForm.company = info.company;
                  ruleForm.nickname = info.nickname ? info.nickname : info.company;
                  ruleForm.accountId = info.accountId;
                  ruleForm.contacts = info.contacts;
                  ruleForm.mail = info.mail;
                  ruleForm.phone = info.phone;
                  ruleForm.taxNo = info.taxNo;
                  if(userInfo.type===1) {
                    ruleForm.account = info.account;
                  }else{
                    ruleForm.account = userInfo.account;
                  }
                }
              }
            }
        );
};

/**
 * 弹出重置窗口
 */
const showResetPassDialog = () => {
  ruleForm.showResetPass = true;
};

/**
 * 重置密码
 */
const resetPass = () => {
  if(resetPassForm.password!=resetPassForm.password2){
    ElMessage.error('两次输入的密码不一致');
    return;
  }
  baseService
      .post("/fii/front/account/resetPwd", {id: ruleForm.accountId, password: resetPassForm.password})
      .then((res) => {
            if (res.code === 0) {
              ruleForm.showResetPass = false
            }
          }
      );
};


queryCompanyInfo();

</script>

<style scoped lang="scss">
.global-ruleForm {
	padding: 44px 44px 24px;
	.classroom {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
	}

	.comment {
		color: #4f5a70;
		display: flex;
		align-items: center;
		img {
			width: 16px;
			height: 16px;
			display: block;
			cursor: pointer;
			margin-left: 16px;
		}
	}
	.no_pwd{
		display: inline-block;
		margin-left: 21px;
		font-size: 16px;
		font-weight: 400;
		color: #00A0E9;
	}
}
</style>
